{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.6 \u5904\u7406\u591a\u4e2a\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\u53ef\u80fd\u4f1a\u629b\u51fa\u591a\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u600e\u6837\u624d\u80fd\u4e0d\u521b\u5efa\u5927\u91cf\u91cd\u590d\u4ee3\u7801\u5c31\u80fd\u5904\u7406\u6240\u6709\u7684\u53ef\u80fd\u5f02\u5e38\u5462\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53ef\u4ee5\u7528\u5355\u4e2a\u4ee3\u7801\u5757\u5904\u7406\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    client_obj.get_url(url)\nexcept (URLError, ValueError, SocketTimeout):\n    client_obj.remove_url(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5143\u7956\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u65f6\u90fd\u4f1a\u6267\u884c remove_url() \u65b9\u6cd5\u3002\n\u5982\u679c\u4f60\u60f3\u5bf9\u5176\u4e2d\u67d0\u4e2a\u5f02\u5e38\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u5c06\u5176\u653e\u5165\u53e6\u5916\u4e00\u4e2a except \u8bed\u53e5\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    client_obj.get_url(url)\nexcept (URLError, ValueError):\n    client_obj.remove_url(url)\nexcept SocketTimeout:\n    client_obj.handle_url_timeout(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u7684\u5f02\u5e38\u4f1a\u6709\u5c42\u7ea7\u5173\u7cfb\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u53ef\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u4e00\u4e2a\u57fa\u7c7b\u6765\u6355\u83b7\u6240\u6709\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept (FileNotFoundError, PermissionError):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u88ab\u91cd\u5199\u4e3a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept OSError:\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "OSError \u662f FileNotFoundError \u548c PermissionError \u5f02\u5e38\u7684\u57fa\u7c7b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5904\u7406\u591a\u4e2a\u5f02\u5e38\u672c\u8eab\u5e76\u6ca1\u4ec0\u4e48\u7279\u6b8a\u7684\uff0c\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u4f7f\u7528 as \u5173\u952e\u5b57\u6765\u83b7\u5f97\u88ab\u629b\u51fa\u5f02\u5e38\u7684\u5f15\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept OSError as e:\n    if e.errno == errno.ENOENT:\n        logger.error('File not found')\n    elif e.errno == errno.EACCES:\n        logger.error('Permission denied')\n    else:\n        logger.error('Unexpected error: %d', e.errno)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c e \u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u88ab\u629b\u51fa\u7684 OSError \u5f02\u5e38\u5b9e\u4f8b\u3002\n\u8fd9\u4e2a\u5728\u4f60\u60f3\u66f4\u8fdb\u4e00\u6b65\u5206\u6790\u8fd9\u4e2a\u5f02\u5e38\u7684\u65f6\u5019\u4f1a\u5f88\u6709\u7528\uff0c\u6bd4\u5982\u57fa\u4e8e\u67d0\u4e2a\u72b6\u6001\u7801\u6765\u5904\u7406\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u65f6\u8fd8\u8981\u6ce8\u610f\u7684\u65f6\u5019 except \u8bed\u53e5\u662f\u987a\u5e8f\u68c0\u67e5\u7684\uff0c\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4f1a\u6267\u884c\u3002\n\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u6784\u9020\u591a\u4e2a except \u540c\u65f6\u5339\u914d\u7684\u60c5\u5f62\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('missing')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open('missing')\nexcept OSError:\n    print('It failed')\nexcept FileNotFoundError:\n    print('File not found')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684 FileNotFoundError \u8bed\u53e5\u5e76\u6ca1\u6709\u6267\u884c\u7684\u539f\u56e0\u662f OSError \u66f4\u4e00\u822c\uff0c\u5b83\u53ef\u5339\u914d FileNotFoundError \u5f02\u5e38\uff0c\n\u4e8e\u662f\u5c31\u662f\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u3002\n\u5728\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u5982\u679c\u4f60\u5bf9\u67d0\u4e2a\u7279\u5b9a\u5f02\u5e38\u7684\u7c7b\u6210\u5c42\u7ea7\u5173\u7cfb\u4e0d\u662f\u5f88\u786e\u5b9a\uff0c\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u8be5\u5f02\u5e38\u7684 __mro__ \u5c5e\u6027\u6765\u5feb\u901f\u6d4f\u89c8\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "FileNotFoundError.__mro__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u5217\u8868\u4e2d\u4efb\u4f55\u4e00\u4e2a\u76f4\u5230 BaseException \u7684\u7c7b\u90fd\u80fd\u88ab\u7528\u4e8e except \u8bed\u53e5\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}